package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.MeasureType;
import com.csjy.migration.hnnd.dict.PersonsRelationshipType;
import com.csjy.migration.hnnd.dict.SonMeasureType;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
@BusinessDataConverter(
        insertSql = "INSERT INTO  ND_MEASURE_DETAILS_TABLE ( ID ,  CREATE_NAME ,  CREATE_BY ,  CREATE_DATE ,  UPDATE_NAME ,  UPDATE_BY ,  UPDATE_DATE ,  SYS_ORG_CODE ,  SYS_COMPANY_CODE ,  BPM_STATUS ,  PROJECT_NAME ,  ASSOCIATED_PERSON ,  MEASURE_TYPE ,  MEASURE_TYPE2 ,  ASSOCIATE_NAME ,  ASSOCIATE_ID_NUMBER ,  ASSOCIATION_TYPE_PERSON ,  ASSOCIATION_TYPE_LEGAL ,  LEGAL_PERSON_NAME ,  CERTIFICATE_NUMBER ,  LEGAL_REPRESENTATIVE ,  LEGAL_ID_NUMBER ,  RELATIONSHIP_TYPE ,  ADDRESS ,  AREA ,  WARRANT_NUMBER ,  COMPANY ,  STOCK_RATIO ,  MEASURE_ID ,  COOPERATIVE_INSTITUTION1 ,  COOPERATIVE_INSTITUTION2 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE ND_MEASURE_DETAILS_TABLE SET CREATE_NAME = ?, CREATE_BY = ?, CREATE_DATE = ?, UPDATE_NAME = ?, UPDATE_BY = ?, UPDATE_DATE = ?, SYS_ORG_CODE = ?, SYS_COMPANY_CODE = ?, BPM_STATUS = ?, PROJECT_NAME = ?, ASSOCIATED_PERSON = ?, MEASURE_TYPE = ?, MEASURE_TYPE2 = ?, ASSOCIATE_NAME = ?, ASSOCIATE_ID_NUMBER = ?, ASSOCIATION_TYPE_PERSON = ?, ASSOCIATION_TYPE_LEGAL = ?, LEGAL_PERSON_NAME = ?, CERTIFICATE_NUMBER = ?, LEGAL_REPRESENTATIVE = ?, LEGAL_ID_NUMBER = ?, RELATIONSHIP_TYPE = ?, ADDRESS = ?, AREA = ?, WARRANT_NUMBER = ?, COMPANY = ?, STOCK_RATIO = ?, MEASURE_ID = ?, COOPERATIVE_INSTITUTION1 = ?, COOPERATIVE_INSTITUTION2 = ? WHERE ID = ?",
        findMigratedIdListSql = "select t1.id FROM ND_MEASURE_DETAILS_TABLE t1 left join ND_COUNTER_GUARANTEE_MEASURES t2 on t1.ID = t2.MEASURE_DETAILS_ID  where t2.project_id = ? ",
        auto = false,
        seq = 202
)
public class NdMeasureDetailsTable extends AbstractBusiness{

    public NdMeasureDetailsTable(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }


    @Override
    public void migrate() throws Exception {

    }


    public void customMigrate(Map row) throws SQLException {
        int needMigratedResult = this.needMigrated(buildId(row));
        if(needMigratedResult == 0) {
            return;
        }
        if(needMigratedResult == 1) {
      this.targetDbUtil.updateByParams(updateSql,this.buildValues(row),true);
//            this.batchUpdateValues.add(this.buildValues(row));
        } else {
      this.targetDbUtil.updateByParams(insertSql,this.buildValues(row));
//            this.batchInsertValues.add(this.buildValues(row));
        }
    }

    public List buildValues(Map btItemAssureSecurity){
        List<Object> values = new ArrayList<>();

        //        ID	主键
        values.add(this.buildId(btItemAssureSecurity));
        //        CREATE_NAME	创建人名称
        values.add(null);
        //        CREATE_BY	创建人登录名称
        values.add(null);
        //        CREATE_DATE	创建日期
        values.add(null);
        //        UPDATE_NAME	更新人名称
        values.add(null);
        //        UPDATE_BY	更新人登录名称
        values.add(null);
        //        UPDATE_DATE	更新日期
        values.add(null);
        //        SYS_ORG_CODE	所属部门
        values.add(null);
        //        SYS_COMPANY_CODE	所属公司
        values.add("002");
        //        BPM_STATUS	流程状态
        values.add(1);

        //        PROJECT_NAME	项目名称 新系统数据为空
        values.add(null);

        //        ASSOCIATED_PERSON	关联人 旧系统：101:个人,1-企业,null:旧系统直接不展示相关信息;新系统:自然人,本人,法人
        values.add(
                btItemAssureSecurity.get("OWNERTYPE") == null ? null : (
                        this.sourceData.getBtEnterpriseBasic().get(0).get("PRONAME").equals(btItemAssureSecurity.get("OWNERNAME").toString()) ? "本人" :(
                                "1".equals(btItemAssureSecurity.get("OWNERTYPE").toString()) ? "法人" : "自然人"
                        )));

        //------------------------ 处理反担保措施类别 ----------------------
        // 旧系统分类
        String wayType = btItemAssureSecurity.get("WAYTYPE").toString();
        String oldDictName = BusinessTool.getOldDictDataNameByDictCodeAndDataCode("24", wayType);
        // 反担保措施
        String measureType = "";
        // 二级措施
        String measureType2 = "---请选择---";

        // 如果包含-,则是二级分类
        if(oldDictName.contains("-")){
            String[] dictNameArray = oldDictName.split("-");
            measureType = DictTool.getTargetDictCode(MeasureType.class,dictNameArray[0]);
            measureType2 = DictTool.getTargetDictCode(SonMeasureType.class,oldDictName);
        }else{
            measureType = DictTool.getTargetDictCode(MeasureType.class,oldDictName);
        }

        //------------------------ 处理反担保措施类别 end  ----------------------

        //        MEASURE_TYPE	反担保措施类别
        values.add(measureType);
        //        MEASURE_TYPE2	二级措施
        values.add(measureType2);
        //        ASSOCIATE_NAME	个人姓名
        values.add(btItemAssureSecurity.get("OWNERNAME"));
        //        ASSOCIATE_ID_NUMBER	个人身份证号码
        values.add(btItemAssureSecurity.get("BUSIREGISTCODE"));


        // 关联人主键信息
        String associationTypePersonId = null;//自然人表id
        String associationTypeLegalId = null;// 法人表id

        //        ASSOCIATION_TYPE_PERSON	关联类型（自然人）
        values.add(DictTool.getTargetDictCode(PersonsRelationshipType.class,btItemAssureSecurity.get("RELATIONTYPE")));
        //        ASSOCIATION_TYPE_LEGAL	关联类型（法人）
        values.add(DictTool.getTargetDictCode(PersonsRelationshipType.class,btItemAssureSecurity.get("RELATIONTYPE")));
        //        LEGAL_PERSON_NAME	法人名称 todo 坑点 法人名称只有是企业时,和企业名一致
        values.add( btItemAssureSecurity.get("OWNERTYPE") != null && "1".equals(btItemAssureSecurity.get("OWNERTYPE").toString()) ?
                btItemAssureSecurity.get("OWNERNAME") : null);
        //        CERTIFICATE_NUMBER	营业执照号码
        values.add( (btItemAssureSecurity.get("OWNERTYPE") != null && "1".equals(btItemAssureSecurity.get("OWNERTYPE").toString())) ? btItemAssureSecurity.get("BUSIREGISTCODE") : null) ;
        //        LEGAL_REPRESENTATIVE	法定代表人
        values.add(btItemAssureSecurity.get("CORPORATION"));
        //        LEGAL_ID_NUMBER	法定代表人身份证号码
        values.add(btItemAssureSecurity.get("CORPORATIONCODE"));
        //        RELATIONSHIP_TYPE	经营关系类型
        values.add(btItemAssureSecurity.get("RELATIONTYPE"));
        //        ADDRESS	地址
        values.add(btItemAssureSecurity.get("OWNERADDRESS"));
        //        AREA	面积 旧系统无数据
        values.add(null);
        //        WARRANT_NUMBER	权证号 旧系统无数据
        values.add(null);

        Object company = null;
        if("1".equals(btItemAssureSecurity.get("OWNERTYPE"))){
            company = btItemAssureSecurity.get("OWNERTYPE");
        }
        //        COMPANY	公司 旧系统
        values.add(company);
        //        STOCK_RATIO	股票比例 旧系统无数据
        values.add(null);
        //        MEASURE_ID	反担保措施ID
        values.add(btItemAssureSecurity.get("measureDetailsId"));
        //        COOPERATIVE_INSTITUTION1	合作机构 旧系统无此数据
        values.add(null);
        //        COOPERATIVE_INSTITUTION2	机构分公司 旧系统无数据
        values.add(null);

        return values;
    }


    public String buildId(Map btItemAssureSecurity){
        // 反担保措施表详情表业务数据主键
        String originalId = btItemAssureSecurity.get("DATUMNO").toString()+ "-JW";
        return originalId;
    }


}
